//
//  SettingViewController.m
//  FootPrint
//
//  Created by alex tao on 2/25/13.
//  Copyright (c) 2013 alex tao. All rights reserved.
//

#import "SettingViewController.h"

#import "FeedBackControllerViewController.h"
#import "UserInfoViewController.h"

#import "PickIconViewController.h"
#import "EventDetailViewController.h"
#import "ReminderViewController.h"
#import "PushManageViewController.h"
#import "PoiFrameViewController.h"

@interface SettingViewController ()

@end

@implementation SettingViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        _parser = [[UserInfoParser alloc] init];
        [_parser setDelegate:self];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self reqUserInfo];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reqUserInfo) name:NOTIFY_LOGIN_SUCCESS object:nil];
    
    
    UITapGestureRecognizer *tapGr = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionUserInfo:)] autorelease];
    tapGr.cancelsTouchesInView = NO;
    [_viewSetting addGestureRecognizer:tapGr];
    
    UITapGestureRecognizer *tapGr2 = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionFocus:)] autorelease];
    tapGr2.cancelsTouchesInView = NO;
    [_viewFocus addGestureRecognizer:tapGr2];
    
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reqUserInfo) name:NOTIFY_USER_INFO_REFRESH object:nil];
    
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)didFinishUserInfo:(UserQueryByIdResponseMessage*)userMsg{
    UserMessage* user = [userMsg user];
    NSString* name = user.nickName;
    NSString* sign = user.signName;
    NSString* url = user.portrait.imageUrl;
    _textUserName.text = name;
    _textUserSign.text = sign;
    [_imgUserPhoto loadAsyncImage:url];
}

- (void)failUser{
    [[[iToast makeText:@"失败"] setDuration:2000] show];
}


- (void)reqUserInfo{

    [_parser reqUserInfo:[GConfig getUserId]];
}

- (void)dealloc {
    [_btnMenu release];
    [_textUserName release];
    [_textUserSign release];
    [_imgUserPhoto release];
    [_viewSetting release];
    [_viewFocus release];
    [super dealloc];
}

- (void)viewDidUnload {
    [self setBtnMenu:nil];
    [self setTextUserName:nil];
    [self setTextUserSign:nil];
    [self setImgUserPhoto:nil];
    [self setViewSetting:nil];
    [self setViewFocus:nil];
    [super viewDidUnload];
}

- (IBAction)showSheet:(id)sender {
    
    UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                              delegate:self
                                                     cancelButtonTitle:@"取消"
                                                destructiveButtonTitle:nil
                                                     otherButtonTitles:@"意见反馈", @"退出", nil];
    UIViewController * rootVc = ROOT_VC;
    [actionSheet showInView:rootVc.view];
    [actionSheet release];
}

- (IBAction)clickMerchant:(id)sender {
    PoiFrameViewController* userVC = [[[PoiFrameViewController alloc]init] autorelease];
    [userVC setMode:0];
    
    UINavigationController * navVc = self.navigationController;
    if (nil == navVc) {
        navVc = [[[UINavigationController alloc] initWithRootViewController:userVC] autorelease];
        navVc.navigationBarHidden = YES;
        [ROOT_VC presentModalViewController:navVc animated:YES];
    } else {
        [navVc pushViewController:userVC animated:YES];
    }

}


- (void)actionUserInfo : (UITapGestureRecognizer*)tapGr{

    UIViewController* userVC = [[[UserInfoViewController alloc]init] autorelease];
    
    UINavigationController * navVc = self.navigationController;
    if (nil == navVc) {
        navVc = [[[UINavigationController alloc] initWithRootViewController:userVC] autorelease];
        navVc.navigationBarHidden = YES;
        [ROOT_VC presentModalViewController:navVc animated:YES];
    } else {
        [navVc pushViewController:userVC animated:YES];
    }
}
- (void)actionFocus: (UITapGestureRecognizer*)tapGr{

    UIViewController* userVC = [[[PushManageViewController alloc]init] autorelease];
    
    UINavigationController * navVc = self.navigationController;
    if (nil == navVc) {
        navVc = [[[UINavigationController alloc] initWithRootViewController:userVC] autorelease];
        navVc.navigationBarHidden = YES;
        [ROOT_VC presentModalViewController:navVc animated:YES];
    } else {
        [navVc pushViewController:userVC animated:YES];
    }

}



- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString * title = [actionSheet buttonTitleAtIndex:buttonIndex];
    if([title isEqualToString:@"意见反馈"]){
        UIViewController* feedVC = [[[FeedBackControllerViewController alloc]init] autorelease];
        //[self.navigationController pushViewController:feedVC animated:YES];
       // [self presentModalViewController:feedVC animated:YES];
        
        UINavigationController * navVc = self.navigationController;
        if (nil == navVc) {
            navVc = [[[UINavigationController alloc] initWithRootViewController:feedVC] autorelease];
            navVc.navigationBarHidden = YES;
            [ROOT_VC presentModalViewController:navVc animated:YES];
        } else {
            [navVc pushViewController:feedVC animated:YES];
        }
    } else if([title isEqualToString:@"退出"]){
        [GConfig logout];
    }
    
}

- (IBAction)manageTemplate:(id)sender {
    TemplateViewController * vc = [[[TemplateViewController alloc] initWithNibName:@"TemplateViewController" bundle:nil] autorelease];
    UINavigationController * navVc = [[[UINavigationController alloc] initWithRootViewController:vc] autorelease];
    navVc.navigationBarHidden = YES;
    vc.selDelegate = self;
    [ROOT_VC presentModalViewController:navVc animated:YES];
}

- (IBAction)manageImage:(id)sender {
    PickIconViewController * vc = [[[PickIconViewController alloc] initWithNibName:@"PickIconViewController" bundle:nil] autorelease];
    UINavigationController * navVc = [[[UINavigationController alloc] initWithRootViewController:vc] autorelease];
    navVc.navigationBarHidden = YES;
    [ROOT_VC presentModalViewController:navVc animated:YES];
}

- (IBAction)manageReminder:(id)sender {
    ReminderViewController * vc = [[[ReminderViewController alloc] initWithNibName:@"ReminderViewController" bundle:nil] autorelease];
    vc.reminderMode = eReminderModifyMode;
    UINavigationController * navVc = [[[UINavigationController alloc] initWithRootViewController:vc] autorelease];
    navVc.navigationBarHidden = YES;
    [ROOT_VC presentModalViewController:navVc animated:YES];
}

#pragma mark - TemplateSelDelegate

- (BOOL)didSelTemplate:(EventMessage*)event
{
    if (event)
    {
        UIViewController * rootVc = ROOT_VC;
        
        [rootVc dismissModalViewControllerAnimated:NO];
        
        EventDetailViewController * detail = [[[EventDetailViewController alloc] initWithNibName:@"EventDetailViewController" bundle:nil] autorelease];
        UINavigationController * navVc = [[[UINavigationController alloc] initWithRootViewController:detail] autorelease];
        navVc.navigationBarHidden = YES;
        [rootVc presentModalViewController:navVc animated:YES];
        
        [detail createWithTemplate:event];
    }
    return NO;
}



- (void)didFinishModify:(UserProModifyResponseMessage*)result{

}
- (void)failModify{

}


@end
